//
//  HMOAuthModel.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/8/4.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit

class HMOAuthModel: NSObject, NSCoding {
    
    /*
     "access_token" = "2.00lT3sUGqk9txD9741ff17f80tgAcy";
     "expires_in" = 157679999;
     "remind_in" = 157679999;
     uid = 5953043197;
     */
    
    
    // 第一段获取
    var access_token :String?
    var expires_in :Double = 0 {
        
        didSet {
            
            // expiresDate = 当前时间 + expires_in(秒)
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
        }
        
    }
    var remind_in :String?
    var uid :String?
    
    // 第二段获取
    // 当前用户的昵称
    var screen_name: String?
    // 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    
    // 自己计算
    // 代表过期时间
    var expiresDate: NSDate?
    
    
    
    // 初始化dict 字典转模型
    init(dict: [String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    // 重写desc属性（计算性属性）改变打印效果
    
    override var description: String {
        
        let keys = ["access_token","expires_in","remind_in","uid","avatar_large","screen_name"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    // 归档 存储
    func encodeWithCoder(encoder: NSCoder) {
        encoder.encodeObject(uid, forKey: "uid")
        encoder.encodeObject(access_token, forKey: "access_token")
        encoder.encodeObject(remind_in, forKey: "remind_in")
        encoder.encodeObject(screen_name, forKey: "screen_name")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        encoder.encodeDouble(expires_in, forKey: "expires_in")
        encoder.encodeObject(expiresDate, forKey: "expiresDate")
    }
    
    // 解档 读取 因为是读取，所以是init方法
    required init?(coder decoder: NSCoder) {
        uid = decoder.decodeObjectForKey("uid") as? String
        access_token = decoder.decodeObjectForKey("access_token") as? String
        remind_in = decoder.decodeObjectForKey("remind_in") as? String
        screen_name = decoder.decodeObjectForKey("screen_name") as? String
        avatar_large = decoder.decodeObjectForKey("avatar_large") as? String
        expires_in = decoder.decodeDoubleForKey("expires_in")
        expiresDate = decoder.decodeObjectForKey("expiresDate") as? NSDate
    }
    
    

}
